package com.hmsm.sys.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import com.hmsm.sys.bean.LocalUser;
import com.hmsm.sys.bean.RTXDeptBean;
import com.lzyyj.gmis.base.webui.ICurrentUserAware;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONObject;

@SuppressWarnings({ "serial", "rawtypes" })
public class OrgTreeAction extends ActionSupport implements SessionAware, ICurrentUserAware<LocalUser>{

	@Resource
	private OrgTreeHelper orgTreeHelper;
	
	private Map<String, Object> session;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	private LocalUser currentUser;

	@Override
	public void setCurrentUser(LocalUser currentUser) {
		this.currentUser = currentUser;
	}
	
	public boolean isSimpleFormat() {
		return orgTreeHelper.isSimpleFormat();
	}

	public void setSimpleFormat(boolean simpleFormat) {
		if (orgTreeHelper!=null){
			orgTreeHelper.setSimpleFormat(simpleFormat);
		}
	}
	
	private Map<String, Object> mapResult = new HashMap<String, Object>();
	
	public Map<String, Object> getMapResult() {
		return mapResult;
	}

	private List<Object> listResult = new ArrayList<Object>();
	
	public List<Object> getListResult() {
		return listResult;
	}

	private Integer id;
	private String nodeType;
	private int depth;
	
	public String getId() {
		return nodeType+id;
	}

	public void setId(String id) {
		if (id!=null){
			String[] strList = id.split("-");
			if (strList.length>0){
				nodeType=strList[0].toLowerCase();
			}
			if (strList.length>1){
				this.id=Integer.parseInt(strList[1]);
			}
		}
	}
	
	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	public OrgTreeAction(){
		
	}
	
	/**
	 * action 加载节点树
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String loadNodes(){
				
		RTXDeptBean rootDept = (RTXDeptBean)session.get(OrgTreeHelper.ROOT_KEY);
		if (rootDept==null){
			this.addActionError("组织机构树未初始化！");
			return ERROR;
		}
		orgTreeHelper.setRootDept(rootDept, currentUser);
		try {
			List<JSONObject> list = orgTreeHelper.getNodes(null, null, null, false, depth, currentUser);
			mapResult.put("success", true);
			mapResult.put("zNodes", list);
		} catch (Exception e) {
			e.printStackTrace();
			mapResult.put("success", false);
			mapResult.put("errorMessage", e.getMessage());
		}
		return "json";
	}
	
	/**
	 * action 加载子节点
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String loadSubNodes(){
		RTXDeptBean rootDept = (RTXDeptBean)session.get(OrgTreeHelper.ROOT_KEY);
		if (rootDept==null){
			this.addActionError("组织机构树未初始化！");
			return ERROR;
		}
		orgTreeHelper.setRootDept(rootDept, currentUser);
		try {
			List<JSONObject> list;
			if ("o".equalsIgnoreCase(nodeType)&&id!=null){
				list = orgTreeHelper.getSubNode(id, null, null, false, 1, currentUser);
			}else{
				list = orgTreeHelper.getNodes(null, null, null, false, 1, currentUser);
			}
			for(JSONObject subNode:list){
				listResult.add(subNode);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "arrayJson";
	}
	
}
